package k.web.cityManage;

import com.alibaba.fastjson.JSON;
import k.HelperClazz;
import k.dao.base.EmpDao;
import k.dao.base.GridDao;
import k.dao.base.OrgDao;
import k.dto.Emp;
import k.dto.Grid;
import k.dto.GridLocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 隔壁老王 on 2018/4/15.
 */
@Controller
@RequestMapping("civilManage")
public class GridController {
    @Autowired
    GridDao gridDao;
    @Autowired
    EmpDao empDao;
    @Autowired
    OrgDao orgDao;
    @RequestMapping(value = "/queryGrids")
    @ResponseBody
    public List<Grid> queryGrids(Model model, HttpServletRequest request) {
        List<Grid> grids =gridDao.queryGrids();
        model.addAttribute("grids",grids);
        return grids;
    }

    @ResponseBody
    @RequestMapping(value = "/queryAllGrid")
    public List<Grid> queryAllGrid() {
        List<Grid> grids =gridDao.queryGrids();
        return grids;
    }
    @RequestMapping(value = "/getGridOwnerList")
    public void getGridOwnerList(String org_id, HttpServletResponse response) {
        List<Emp> emps =empDao.getGridOwnerList(org_id);
        HelperClazz.renderJson(response, JSON.toJSONString(emps));
    }
    @RequestMapping(value = "/queryGridLocation/{gridId}")
    public void queryGridLocation(@PathVariable String gridId, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        List<GridLocation> grid = gridDao.getGridLocation(gridId);
        HelperClazz.renderJson(response, JSON.toJSONString(grid));
    }
    @RequestMapping(value = "/saveGrid")
    public void saveGrid(@ModelAttribute("Grid")Grid grid, HttpServletResponse response) {
        String gridOwner=grid.getEmp_name();
        List<Grid> ownerlist=new ArrayList();
        List<Grid> memList=new ArrayList();
        System.out.println(JSON.toJSONString(grid));
        for(String str:gridOwner.split(",")){
            Grid grid_owner=new Grid();
            String ownerId=str.split(":")[0];
            String ownerName=str.split(":")[1];
            grid_owner.setEmp_id(Integer.parseInt(ownerId));
            grid_owner.setEmp_name(ownerName);
            ownerlist.add(grid_owner);
        }
//        for(String str:gridMem.split(",")){
//            Mz_grid_owner grid_mem=new Mz_grid_owner();
//            String ownerId=str.split(":")[0];
//            String ownerName=str.split(":")[1];
//            grid_mem.setEmp_id(ownerId);
//            grid_mem.setEmp_name(ownerName);
//            grid_mem.setType("2");//网格员
//            memList.add(grid_mem);
//        }
        if(HelperClazz.isEmpty(grid.getGrid_id())){
            int status = gridDao.addGrid(grid, ownerlist, memList);
            HelperClazz.renderJson(response, JSON.toJSONString(status));
        }else{
            int status = gridDao.updGrid(grid, ownerlist, memList);
            HelperClazz.renderJson(response,JSON.toJSONString(status));
        }
    }
    @RequestMapping(value = "/addGridLocation/{grid}")
    public void addGridLocation(@PathVariable String grid,HttpServletRequest request,HttpServletResponse response) {
//        gridDao.deleteGridLocation(grid);
        String xlocation = request.getParameter("xlocation");
        String ylocation = request.getParameter("ylocation");
        String seq = request.getParameter("seq");
        GridLocation gridLocation = new GridLocation();
        gridLocation.setGrid_location_x(xlocation);
        gridLocation.setGrid_location_y(ylocation);
        gridLocation.setSeq(seq);
        gridLocation.setGrid_id(grid);
        int status  = gridDao.addGridLocation(gridLocation);
        HelperClazz.renderJson(response, ""+status);
    }

    @RequestMapping(value = "/gridIndex")
    public String gridIndex(Model model,HttpServletRequest request) {
        orgDao.findAll();
        model.addAttribute("org_ids", orgDao.findAll());
        return "system_manage/grid";
    }
}
